<template>
	<view class="orderList">
		<view class="top">
			<view class="" @tap="topChange(item)" :class="data.obj.orderState == item.id?'active':''"
				v-for="(item,index) in data.topArr" :key="index">
				<text>{{item.n}}</text>
			</view>
		</view>
		<scroll-view :refresher-triggered="data.refresherBool" :refresher-enabled="true"
			@refresherrefresh="refresherrefresh"  @scrolltolower="scrolltolower" scroll-y="true">
			<view class="listItem1" v-for="(item,index) in data.result.list" :key="index" @tap="navTo(item)">
				<Completed @handeDelete="handeDelete(index)" :orderState="data.obj.orderState" v-if="item.orderState.value == 'COMPLETE'" :item="item"/>
				<PendingShipment :orderState="data.obj.orderState" v-if="item.orderState.value == 'WAIT_SEND'" :item="item"/>
				<PendingReceiptGoods @handeDelete="handeDelete(index)" :orderState="data.obj.orderState" v-if="item.orderState.value == 'WAIT_RECEIVE'" :item="item"/>
				<Obligation :index="index" @pay="handePay" :orderState="data.obj.orderState" v-if="item.orderState.value == 'WAIT_PAY'" :item="item"/>
				<Cancelled :orderState="data.obj.orderState" v-if="item.orderState.value == 'CANCEL'" :item="item"/>
			</view>
		</scroll-view>
		<u-popup :round="20" :zIndex="9999" :show="data.show" @close="data.show = false">
			<view class="confirmOrderBottom">
				<view class="top">
					<text></text>
					支付
					<image @tap="data.show = false" src="/static/img/index/101.png" mode="widthFix"></image>
				</view>
				<view class="confirmOrderBottom_1">
					<view v-for="(item,index) in paymentChannels" :key="index" class="" @tap="data.payChannel = item.value">
						<view class="left">
							<image :src="'/static/img/index/'+item.icon+'.png'" mode="widthFix"></image>
							{{item.name}}
						</view>
						<image :src="data.payChannel == item.value?'/static/img/index/49.png':'/static/img/index/52.png'" mode="widthFix">
						</image>
					</view>
				</view>
				<view class="bot">
					<view class="left">
						实付现金（元）
						<text>{{data.payInfo.totalPayAmount}}</text>
					</view>
					<view class="right" @tap="pay">
						立即支付
					</view>
				</view>
			</view>
		
		</u-popup>
	</view>
</template>

<script lang="ts" setup>
	import Completed from './components/completed'
	import PendingShipment from './components/pendingShipment'
	import PendingReceiptGoods from './components/pendingReceiptGoods'
	import Obligation from './components/obligation'
	import Cancelled from './components/cancelled'
	import {paymentChannels} from '@/utils/index'
	import {
		reactive,
	} from 'vue';
	import { buyerOrderPage,payOrder } from '@/utils/api/goods'
	const data = reactive<any>({
		topArr: [{ n: '全部', id: '' }, { n: '待付款', id: 'WAIT_PAY' }, { n: '待收货', id: 'WAIT_RECEIVE' }, { n: '已完成', id: 'COMPLETE' }, { n: '已取消', id: 'CANCEL' }],
		index: 0,
		obj: { pageSize: 10, pageNo: 1, orderState: '' },
		result: {},
		httpUrl: getApp().globalData.baseUrl,
		payChannel:paymentChannels[0].value,
		payInfo:{}
	})
	const pay=()=>{
		payOrder({data:{orderNo:data.payInfo.groupOrderNo,payChannel:data.payChannel}}).then((res)=>{
			if(res.code == 200){
				uni.requestPayment({
				    provider: 'alipay',
				    orderInfo: res.data.externalData, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
				    success: function (res) {
				        console.log('success:' + JSON.stringify(res));
				    },
				    fail: function (err) {
				        console.log('fail:' + JSON.stringify(err));
				    }
				});
			}
		})
	}
	const handePay=({item,index})=>{
		console.log(item,'item')
		data.show = true;
		data.payInfo = item;
		data.payInfo.index = index;
	}
	const handeDelete=(index)=>{
		data.result.total = data.result.total - 1;
		data.result.list.splice(index,1)
	}
	const requstList = () => {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		if (data.obj.pageNo == 1) {
			data.result.list = [];
		}
		buyerOrderPage({ data: data.obj }).then((res) => {
			res.data.list = [...data.result.list, ...res.data.list];
			data.result = res.data;
			data.refresherBool = false;
			uni.hideLoading()
		})
	}
	const navTo=(item)=>{
		uni.navigateTo({
			url:'/pages/lifeOrderDetails/index?groupOrderNo='+item.groupOrderNo+'&orderState='+item.orderState.value
		})
	}
	const refresherrefresh = () => {
		data.refresherBool = true;
		data.obj.pageNo = 1;
		requstList()
	}
	const topChange=(item)=>{
		if(item.id != data.obj.orderState){
			data.obj.pageNo = 1;
			data.obj.orderState = item.id;
			requstList()
		}
	}
	const scrolltolower=()=>{
		if(data.result.total != data.result.list.length){
			data.obj.pageNo += 1;
			requstList()
		}
	}
	requstList()
</script>

<style lang="scss">
	.confirmOrderBottom{
		padding: 24rpx 43rpx;
		.top{
			display: flex;
			align-items: center;
			justify-content: space-between;
			image{
				width: 30rpx;
			}
			text{
				width: 30rpx;
			}
			font-size: 32rpx;
		}
		.bot{
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 30rpx;
			.left{
				display: flex;
				flex-direction: column;
				font-size: 24rpx;
				text{
					margin-top: 10rpx;
					color: #FF263F;
				}
			}
			.right{
				width: 190rpx;
				line-height: 80rpx;
				background:  #FF263F;
				text-align: center;
				border-radius: 80rpx;
				color: #fff; 
				
			}
		}
	}
	.confirmOrderBottom_1 {
		>view {
			justify-content: space-between;
			// padding: 37rpx 35rpx;
			background: #fff;
			display: flex;
			align-items: center;
			margin-top: 32rpx;
	
			.left {
				display: flex;
				align-items: center;
	
				image {
					width: 48rpx;
					margin-right: 25rpx;
				}
	
				font-size: 24rpx;
			}
	
			>image {
				width: 36rpx;
			}
		}
	}
	.listItem1{
		padding: 24rpx;
		border-radius: 24rpx;
		background: #fff;
		margin-bottom: 24rpx;
	}
	scroll-view{
		padding: 20rpx 0;
		flex: 1;
		overflow: auto;
		
	}
	.orderList {
		height: 100vh;
		background: #F4F6FA;
		display: flex;
		flex-direction: column;
		>.top {
			width: 100%;
			background: #fff;
			padding: 20rpx 30rpx 0 30rpx;
			display: flex;
			justify-content: space-between;
			view {
				&::after {
					content: '';
					display: block;
					width: 40rpx;
					height: 4rpx;
					border-radius: 4rpx;
					// background: ;
					margin: 10rpx auto 0 auto;
				}

				text {
					color: #595959;
				}

				display: inline-block;
				padding: 0 24rpx;

			}

			.active {
				&::after {

					background: #6B64D3;
				}

				text {
					color: #6B64D3;
				}
			}
		}

	}


</style>